import 'package:flutter/material.dart' hide Drawer;
import 'dart:async';
import 'package:hairen_app/pages/plug/drawer.dart';
import 'package:hairen_app/server/server.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:path/path.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:flutter_web_browser/flutter_web_browser.dart';
import 'package:hairen_app/server/state/UserInfoModel.dart';


class Version extends StatefulWidget{
  
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new _VersionState() ;
  }
}

class _VersionState extends State<Version>{
  bool  isupdate=true;
  String  info="";
  BuildContext context;
  @override
  initState(){
    super.initState();
    updateVersion();
  }
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }
  //版本检测
  updateVersion() async {
    Map data=await  Server.getVersion();
    if(data['code']==1){
      if(double.parse(data['data']['version'])>UserInfoModel.version){
        alertVersion(data['data']['file']);
        setState(() {
          info="最新版本${data['data']['version']}";
        });

      }else{
        Fluttertoast.showToast(
          msg:"你已经是最新版本啦！",
          gravity: ToastGravity.TOP,
          toastLength: Toast.LENGTH_SHORT,
          timeInSecForIos: 1,
        );
        setState(() {
          info="当前版本${UserInfoModel.version}";
        });
      }
    }
  }

  Future alertVersion(String path) async{
    return   showDialog<Null>(
      context: context,
      barrierDismissible: false, // user must tap button!
      builder: (BuildContext context) {
        return new AlertDialog(
          title: new Text("发现新版本,是否更新"),
          actions: <Widget>[
            new Container(
              child:  new RaisedButton(
                child: new Text('确认',style: new TextStyle( color: Colors.white,),),
                onPressed: () {
                  FlutterWebBrowser.openWebPage(url: path, androidToolbarColor: Colors.deepPurple);
                  Navigator.of(context).pop();
                },
              ),
              alignment: Alignment.centerLeft,
            ),
            new RaisedButton(
              child: new Text('取消',style: new TextStyle( color: Colors.white,),),
              color: Colors.black12.withOpacity(0.5),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }
  @override
  Widget build(BuildContext context) {
    this.context=context;
    // TODO: implement build
    return Scaffold(
      drawer: new DrawerPage(),
      appBar: AppBar(
          elevation: 0.0,
          title: new Text("版本更新"),
          leading: new Builder(builder: (context){
            return new IconButton(icon: new Icon(Icons.menu), onPressed:(){
              Scaffold.of(context).openDrawer();
            });
          }),
         ),
        body:Align(
          alignment: Alignment.center,
          child: new Container(
            height: 200.0,
            child: new Align(
              child: new Column(
                children: <Widget>[
                  new Image.asset(
                    'assets/logo.png',
                    width: 80.0,
                  ),
                  new SizedBox(height: 10.0,),
                  new Text('嗨人视频,快人一步'),
                  new SizedBox(height: 10.0,),
                  new FlatButton(onPressed: (){
                    updateVersion();
                  },child: new Text(info),),

                ],
              ),
            ),
          ),
        )
    );
  }
}